<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <!-- 事件委派 -->
      <div @mouseleave="unBackground" @mouseenter="enterShow">
        <h2 class="all">全部商品分类</h2>
        <!-- 添加动画 -->
        <transition name="sort">
          <div class="sort" v-show="show">
          <!-- 利用事件委派+编程式导航实现路由跳转 -->
          <div class="all-sort-list2" @click="goSearch">
            <!-- 渲染三级分类 -->
            <div class="item" v-for="(c1,index) in categoryList.data" :key="c1.categoryId" :class="{cur:currentIndex===index}">
              <!-- 鼠标移上去事件 -->
              <h3 @mouseenter="Background(index)">
                <!-- 添加自定义属性 当想要获取元素中的某个时 自定义属性性能时特别好的 -->
                <a :data-categoryName="c1.categoryName" :data-category1Id="c1.categoryId">{{ c1.categoryName }}</a>
              </h3>
              <div :style="{display:currentIndex==index?'block':'none'}" class="item-list clearfix" >
                <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                  <dl class="fore">
                    <dt>
                      <a :data-categoryName="c2.categoryName" :data-category2Id="c2.categoryId">{{ c2.categoryName }}</a>
                      <!-- <router-link to="/search">{{ c2categoryName }}</router-link> -->
                    </dt>
                    <dd>
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                        <a :data-categoryName="c3.categoryName" :data-category3Id="c3.categoryId">{{ c3.categoryName }}</a>
                        <!-- <router-link to="/search">{{ c3.categoryName }}</router-link> -->
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">多多购物超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
// 引入 lodash 防抖、节流
import {throttle} from 'lodash'

export default {
  name:'TypeNav',
  data(){
    return{
      currentIndex: -1,
      show:true
    }
  },
  mounted(){
    // console.log(this.$store.state.home.categoryList);
    if(this.$route.path != '/home'){
      this.show = false
    }
  },
  computed:{
    ...mapState('home',['categoryList']),
  },
  methods:{
    // 当鼠标放上去则背景变 浅蓝色 
    // 添加节流 此时只能使用 es5 写法
    // 因为用户操作过快 会导致浏览器反应不过来 
    Background: throttle(function(index){
      this.currentIndex = index
    },50), // 设置50毫秒才会触发一次
    // 鼠标划走时 currentIndex 变回默认值
    unBackground(){
      this.currentIndex = -1
      if(this.$route.path != '/home'){
        this.show = false
      }
    },
    // 事件委派 鼠标进入全部商品时
    enterShow(){
      if(this.$route.path != '/home'){
        this.show = true
      }
    },
    // 事件委派 进行路由跳转
    goSearch(event){
      // 事件委派是最好的解决方案
      let element = event.target
      // console.log(element.dataset); // dataset 获取自定义参数
      // 对象结构赋值
      let {categoryname, category1id, category2id, category3id} = element.dataset
      // console.log(categoryname);
      let location = {name:'search'}
      if(categoryname) {
        
        // 设置query参数
        let query = { categoryName: categoryname}
        if(category1id){
          query.category1Id = category1id
        }else if(category2id){
          query.category2Id = category2id
        }else{
          query.category3Id = category3id
        }
        // console.log(location,query);
        // 整理参数
        // location 创建一个query 将 query 里面的数据放到 location 里面
        let params = {keyword: this.$route.params}
        // console.log('------------',params)
        location.params = params
        if(this.$route.params.keyword){
          location.params = this.$route.params
          // console.log('test',this.$route.params);
        }
        location.query = query
        this.$router.push(location)
        
        // 路由跳转
        
        // 以下代码简写
        // this.$router.push({
        //   name:'search',
        //   params:{
        //     k:111
        //   },
        //   query:{
        //     categoryName:categoryname,
        //     cateid:category1id
        //   }
        // })
      }

    }
  }
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;
  
  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }
    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      animation: sort .5s linear;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }
      
          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            font-size: 12px;
            font-weight: 500;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
        // .item:hover .item-list{
        //   display: block!important;
        // }
        .cur {
          background: lightblue;
        }
      }
    }
    // 进入时、结束后动画
    .sort-enter,.sort-leave-to{
      opacity: 0;
    }
    // 进入终点、结束起点
    .sort-enter-to,.sort-leave{
      opacity: 1;
    }
    // 设置动画时间
    .sort-enter-active,.sort-leave-active{
      transition: all .5s linear;
    }
  }
}
</style>
